package com.easy.dn.utils.encryption;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;


public class Des {
	// private static byte[] iv = { 1, 2, 3, 4, 5, 6, 7, 8 };
	public static final String key = "20151113";

	public static String encryptDES(String encryptString, String encryptKey) throws Exception {
		// IvParameterSpec zeroIv = new IvParameterSpec(new byte[8]);
		// IvParameterSpec zeroIv = new IvParameterSpec(iv);
		SecretKeySpec key = new SecretKeySpec(encryptKey.getBytes(), "DES");
		Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
		cipher.init(Cipher.ENCRYPT_MODE, key);
		byte[] encryptedData = cipher.doFinal(encryptString.getBytes());

		return DownResumeBase64.encode(encryptedData);
	}

	public static String decryptDES(String decryptString, String decryptKey) throws Exception {
		byte[] byteMi = new DownResumeBase64().decode(decryptString);
		// IvParameterSpec zeroIv = new IvParameterSpec(iv);
		// IvParameterSpec zeroIv = new IvParameterSpec(new byte[8]);
		SecretKeySpec key = new SecretKeySpec(decryptKey.getBytes(), "DES");
		Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
		cipher.init(Cipher.DECRYPT_MODE, key);
		byte decryptedData[] = cipher.doFinal(byteMi);

		return new String(decryptedData);
	}

	/*********************************
	 * des +base64加密
	 ********************************/
	public static String signData(String content) {
		try {
			return appendData(content);
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}

	private static String appendData(String param1) {
		String sign = "";
		try {
			sign = Des.encryptDES(param1, Des.key);// 加密
			sign = sign.replace("=", "@");
			sign = sign.replace("+", "-");
			sign = sign.replace("/", "_");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sign;
	}
}